package com.genesis.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.OmsCartItem;
import com.genesis.mall.entity.PmsSkuStock;
import com.genesis.mall.entity.UmsAdmin;
import com.genesis.mall.entity.UmsMember;
import com.genesis.mall.service.IOmsCartItemService;
import com.genesis.mall.service.IPmsSkuStockService;
import com.genesis.mall.service.IUmsMemberService;
import com.genesis.mall.utils.TokenUtil;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 购物车表 前端控制器
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@RestController
@RequestMapping("/cart")
public class OmsCartItemController {

    @Autowired
    private IOmsCartItemService omsCartItemService;

    @Autowired
    private IUmsMemberService umsMemberService;

    @Autowired
    private IPmsSkuStockService pmsSkuStockService;

    /**
     * 添加购物车
     * @param authorisation
     * @param omsCartItem
     * @return
     */
    @PostMapping("/add")
    public Result<OmsCartItem>add(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorisation,@RequestBody OmsCartItem omsCartItem){
        String token=authorisation.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long id = Long.parseLong(claims.get("umsMember").toString()) ;
        omsCartItem.setMemberId(id);

        UmsMember umsMember= umsMemberService.getById(id);
        omsCartItem.setMemberNickname(umsMember.getNickname());

        PmsSkuStock pmsSkuStock= pmsSkuStockService.getById(omsCartItem.getProductSkuId());
        if (pmsSkuStock!=null){
            omsCartItem.setProductSkuCode(pmsSkuStock.getSkuCode());
        }


        LambdaQueryWrapper<OmsCartItem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(OmsCartItem::getMemberId,id);
        wrapper.eq(OmsCartItem::getProductId,omsCartItem.getProductId());
        OmsCartItem one= omsCartItemService.getOne(wrapper);

        if (one!=null){
            //购物车存在商品,在原来的数量加一
            Integer number = one.getQuantity();
            one.setQuantity(number+1);
            omsCartItemService.updateById(one);
        }else {
            //不存在,添加到购物车
            omsCartItem.setQuantity(1);
            omsCartItem.setCreateDate(LocalDateTime.now());
            omsCartItemService.save(omsCartItem);
        }

        return Result.success(omsCartItem);

    }

    /**
     * 清空购物车
     * @param authorisation
     * @return
     */
    @PostMapping("/clear")
    private Result<String>clear(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorisation){
        String token=authorisation.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long id = Long.parseLong(claims.get("umsMember").toString()) ;
        LambdaQueryWrapper<OmsCartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsCartItem::getMemberId,id);
        omsCartItemService.remove(queryWrapper);
        return Result.success("购物车清空成功");

    }

    /**
     * 购物车列表
     * @param authorisation
     * @return
     */
    @GetMapping("/list")
    public Result<List<OmsCartItem>>list(@RequestHeader(HttpHeaders.AUTHORIZATION) String authorisation){
        String token=authorisation.substring(6);
        Claims claims = TokenUtil.getClaims(token);
        Long id = Long.parseLong(claims.get("umsMember").toString()) ;
        LambdaQueryWrapper<OmsCartItem> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OmsCartItem::getMemberId,id);
        List<OmsCartItem>list=omsCartItemService.list(queryWrapper);

        return Result.success(list);
    }

    /**
     * 删除购物车里的商品
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public Result<String>delete(@RequestParam Integer ids){

        omsCartItemService.removeById(ids);
        return Result.success("删除成功");

    }

    @GetMapping("/update/quantity")
    public Result<String>update(Long id,Integer quantity){
        LambdaUpdateWrapper<OmsCartItem> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(OmsCartItem::getId,id);
        updateWrapper.set(OmsCartItem::getQuantity,quantity);
        omsCartItemService.update(updateWrapper);

        return Result.success("修改成功");
    }






}
